package com.jay.media.music.core.util

import android.annotation.SuppressLint
import android.media.MediaMetadata
import com.jay.media.music.core.db.Music
import com.jay.media.music.core.provider.METADATA_TRACK_SOURCE

/**
 * @author xuzhou4
 * @date 2022/3/17
 */
object MusicExt {
    @SuppressLint("WrongConstant")
    fun MediaMetadata.toMusic(): Music {
        val music = Music()
        music.id = getString(MediaMetadata.METADATA_KEY_MEDIA_ID)
        music.data = getString(METADATA_TRACK_SOURCE)
        music.displayName = getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE)
        music.title = getString(MediaMetadata.METADATA_KEY_TITLE)
        music.artist = getString(MediaMetadata.METADATA_KEY_ARTIST)
        music.album = getString(MediaMetadata.METADATA_KEY_ALBUM)
        music.duration = getLong(MediaMetadata.METADATA_KEY_DURATION)
        music.albumArt = getString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI) ?: ""
        return music
    }

    @SuppressLint("WrongConstant")
    fun Music.toMediaMetadata(): MediaMetadata {
        return MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, id)
            .putString(METADATA_TRACK_SOURCE, data)
            .putString(MediaMetadata.METADATA_KEY_MEDIA_URI, data)
            .putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, displayName)
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
            .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, albumArt)
            .build()
    }
}
